/**
 * @file 复合指标计算
 * @author liushuang(liushuang04@baijia.com)
 * @date 2019-12-04
 */

/**
 * 复合指标运算 获取
 *
 * @param {data} array 指标list
 * @return {options} {返回运算规则}
 */
export const getCalculations = data => {
    const list = data.map(item => {
        if (item.taskId === 0 && item.calSql && item.calSql !== '') {
            let sqlArr = [];
            const unitReserved = [item.unit, item.reservedNo];
            // sql 解析运算
            const sqlObj = JSON.parse(item.calSql);
            if (sqlObj.calsql) {
                sqlArr = [`field${item.sn}`, '='];

                const sqlArrTemp = sqlObj.calsql.map(i => {
                    if (i.key) {
                        const index = data.findIndex(item => item.measureId === i.key && item.taskId !== 0);
                        if (index > -1) {
                            return `field${data[index].sn}`;
                        }
                        return null;
                    }
                    return i.label;
                });
                // 运算数组
                sqlArr = sqlArr.concat(sqlArrTemp);
            }
            sqlArr = sqlArr.concat(unitReserved);
            return sqlArr;
        }
        return null;
    });
    return list.filter(item => item);
};

/**
 * 复合指标运算
 *
 * @param {data} array 数据list
 * @param {calculations} array 运算
 * @return {options} {返回计算后的数据}
 */
export const covertIndicator = (data, calculations) => {
    data = data.map(item => {
        for (const key in item) {
            if (key.indexOf('field') > -1) {
                for (let i = 0; i < calculations.length; i++) {
                    // 复合指标
                    const computedKey = calculations[i][0];
                    if (key === computedKey) {
                        const temp = calculations[i].map(node => {
                            if (item[node] && node.indexOf('field') > -1) {
                                return item[node] || null;
                            }
                            return node;
                        });
                        const unit = temp[temp.length - 2];
                        const reserved = temp[temp.length - 1];
                        const calStr = temp.slice(2, temp.length - 2).join('');
                        try {
                            // eslint-disable-next-line no-eval
                            const result = eval(calStr);
                            if (isNaN(result)) {
                                item[key] = '-';
                            }
                            else if (unit === '百分比') {
                                item[key] = `${(result * 100).toFixed(reserved)}%`;
                            }
                            else {
                                item[key] = result.toFixed(reserved);
                            }
                        }
                        catch (error) {
                            item[key] = '-';
                        }
                    }
                }
            }
        }
        return item;
    });
    return data;
};

/**
 * 指标数据转换
 *
 * @param {data} array 数据list
 * @return {options} {返回转换后的数据}
 */
export const groupByData = (datas, dateDims, prop) => {
    let keys = [];
    for (let i = 1; i < 17; i++) {
        keys.push(`dimension${i}`);
    }
    keys = keys.filter(ele => ele !== dateDims);
    const list = datas || [];
    const groups = [];
    list.forEach(v => {
        const key = {};
        const data = {};
        keys.forEach(k => {
            key[k] = v[k];
        });
        let group = groups.find(v => v.name === Object.values(key).filter(res => res)
            .join('-'));
        const name = Object.values(key).filter(i => i)
            .join('-');
        if (!group) {
            group = {
                name,
            };
            groups.push(group);
        }
        const point = {
            xAixs: v[dateDims],
            value: v[prop]
        };
        group.point = group.point || [];
        group.point.push(point);
    });
    return groups;
};

